package com.yiren.dbaa.module.common.view.dialog;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.yiren.dbaa.R;
import com.yiren.dbaa.currency.api.CommonCallBack;
import com.yiren.dbaa.currency.model.ScreenModel;
import com.yiren.dbaa.module.common.contract.MainContract;
import com.yiren.dbaa.util.GraphicUtil;
import com.yiren.dbaa.zxing.encoding.ZXingUtils;

/**
 * Describe 学员对话框
 *
 * @author WRX
 * @date 2022/12/9
 */
public class LoginDialog {

    private Activity activity;
    private MainContract.Presenter mainPresenter;
    /*** 学员登录对话框 */
    private AlertDialog studentDialog;
    /*** 教练登录对话框 */
    private AlertDialog coachDialog;
    /*** 教练登录类型1：扫码登录 2：账号密码登录 */
    private int coachLoginType = 1;

    public LoginDialog(Activity activity, MainContract.Presenter mainPresenter) {
        this.activity = activity;
        this.mainPresenter = mainPresenter;
    }

    /**
     * 关闭学员登录对话框
     */
    public void closeStudentLoginDialog() {
        if (null != studentDialog && studentDialog.isShowing()) {
            studentDialog.dismiss();
        }
    }

    /**
     * 显示教练登录对话框
     */
    public void showCoachLoginDialog(String qrCodeUrl, CommonCallBack callBack) {
        coachDialog = new AlertDialog.Builder(activity).create();
        coachDialog.setCancelable(true);
        View view = LayoutInflater.from(activity).inflate(R.layout.dialog_coach_login, null);
        coachDialog.setView(view); //由于AlertDialog中有输入框，设置这一行才能显示出输入框的软键盘
        coachDialog.show();
        WindowManager.LayoutParams params = coachDialog.getWindow().getAttributes();
        ScreenModel screen = GraphicUtil.getResolutionInfo(activity);
        params.width = (int) ((screen.getWidth() * 6) / 10);
//        params.height = (screen.getHeight() * 8) / 10;
        coachDialog.getWindow().setAttributes(params);
        Window window = coachDialog.getWindow();
        window.setContentView(R.layout.dialog_coach_login);
        TextView tvLoginWay = window.findViewById(R.id.tv_login_way);
        LinearLayout llLoginType = window.findViewById(R.id.ll_login_type);
        TextView tvLoginType = window.findViewById(R.id.tv_login_type);
        ImageView ivLoginType = window.findViewById(R.id.iv_login_type);
        LinearLayout llRrCode = window.findViewById(R.id.ll_qr_code);
        ImageView ivCoachCode = window.findViewById(R.id.iv_coach_code);
        LinearLayout llAccount = window.findViewById(R.id.ll_account);
        EditText etAccount = window.findViewById(R.id.et_account);
        EditText etPassword = window.findViewById(R.id.et_password);
        TextView tvLogin = window.findViewById(R.id.tv_login);
        llLoginType.setOnClickListener(v -> {
            coachLoginType = coachLoginType == 1 ? 2 : 1;
            if (coachLoginType == 1) {
                tvLoginWay.setText("扫码登录");
                tvLoginType.setText("账号密码登录");
                ivLoginType.setImageResource(R.mipmap.login_account);
                llRrCode.setVisibility(View.VISIBLE);
                llAccount.setVisibility(View.GONE);
            } else {
                tvLoginWay.setText("短信/密码登录");
                tvLoginType.setText("扫码登录");
                ivLoginType.setImageResource(R.mipmap.login_code);
                llRrCode.setVisibility(View.GONE);
                llAccount.setVisibility(View.VISIBLE);
            }
        });
        Bitmap bitmap = ZXingUtils.createQRImage(qrCodeUrl, 500, 500);
        Glide.with(activity).load(bitmap).into(ivCoachCode);
        tvLogin.setOnClickListener(v -> {
            String phone = etAccount.getText().toString().trim();
            String password = etPassword.getText().toString().trim();
            mainPresenter.accountLogin(phone, password, (MainContract.View) activity);
        });
        coachDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                dialog.dismiss();
                callBack.complete();
            }
        });
    }

    /**
     * 关闭学员登录对话框
     */
    public void closeCoachLoginDialog() {
        if (null != coachDialog && coachDialog.isShowing()) {
            coachDialog.dismiss();
        }
    }

    /**
     * 显示教练已经登录对话框
     */
    public void showAlreadyLoginDialog(String phone, CommonCallBack callBack) {
        AlertDialog alreadyDialog = new AlertDialog.Builder(activity).create();
        alreadyDialog.setCancelable(true);
        alreadyDialog.show();
        WindowManager.LayoutParams params = alreadyDialog.getWindow().getAttributes();
        ScreenModel screen = GraphicUtil.getResolutionInfo(activity);
        params.width = (int) ((screen.getWidth() * 6) / 10);
//        params.height = (screen.getHeight() * 8) / 10;
        alreadyDialog.getWindow().setAttributes(params);
        Window window = alreadyDialog.getWindow();
        window.setContentView(R.layout.dialog_already_login);
        TextView tvPhone = window.findViewById(R.id.tv_phone);
        TextView tvChangeAccount = window.findViewById(R.id.tv_change_account);
        tvPhone.setText(phone);
        tvChangeAccount.setOnClickListener(v -> {
            alreadyDialog.dismiss();
            callBack.complete();
        });
    }
}
